<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增任务调度')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: ztree-css" />
    <th:block th:include="include :: bootstrap-select-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-dispatcher-add">
            <div class="form-group is-required">
                <label class="col-sm-3 control-label is-required">GIT仓库名称：</label>
                <div class="col-sm-8">
                    <select id="repositoryId" name="repositoryId"  class="form-control" required>
                        <option></option>
                        <option th:each="repo:${repoList}" th:value="${repo.repositoryId}" th:text="${repo.repositoryName}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group" id="selectDpName">
                <label class="col-sm-3 control-label is-required">选择文件：</label>
                <div class="col-sm-8">
                    <label class="check-box">
                        <input type="checkbox" value="1">展开/折叠</label>

                    <div id="fileTrees" class="ztree ztree-border"></div>
                </div>
            </div>
            <input name="dpId" id="fileId" type="hidden" required>
            <input name="dpRepoType" id="dpRepoType" value="0" type="hidden" required>
            <div class="form-group is-required">
                <label class="col-sm-3 control-label is-required">调度名称：</label>
                <div class="col-sm-8">
                    <input id="dpName" name="dpName" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group is-required">
                <label class="col-sm-3 control-label is-required">服务节点：</label>
                <div class="col-sm-8">
                    <select id="carteId" name="carteId"  class="form-control" required>
                        <option></option>
                        <option th:each="node:${nodeList}" th:value="${node.nodeId}" th:text="${node.name}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">调度地址：</label>
                <div class="col-sm-8">
                    <input name="dpPath" class="form-control" type="text" id="dpPath" required readonly>
                </div>
            </div>
            <div class="form-group is-required">
                <label class="col-sm-3 control-label">参数组：</label>
                <div class="col-sm-8">
                    <select id="paramsGroupId" name="paramsGroupId"  class="form-control">
                        <option></option>
                        <option th:each="pg:${pgList}" th:value="${pg.paramsGroupId}" th:text="${pg.paramsGroupName}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">预警信息：</label>
                <div class="col-sm-8">
                    <select class="selectpicker" id="warnMsgIds" multiple placeholder="请选择通知">
                        <option>-</option>
                        <option th:each="warn:${warnList}" th:value="${warn.id}">
                            [[*{warn.contactType == '1' ? '短信：'+warn.phoneNum: warn.contactType == '2' ? 'e-mail：'+warn.eMailAddr : '钉钉：'+warn.dingWebHook} ]]
                        </option>
                    </select>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: ztree-js" />
    <th:block th:include="include :: bootstrap-select-js" />
    <script th:inline="javascript">
        var nodeList = [[${nodeList}]]
        var prefix = ctx + "busi/dispatcherGit";
        $("#form-dispatcher-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                let serialize = $('#form-dispatcher-add').serialize();
                let vals = $('#warnMsgIds').val();
                let warnMsgIds = "";
                for (var i = 0; i<vals.length; i++) {
                    if (i === 0 ) {
                        if (vals[i] === '-') {
                            continue;
                        }
                        warnMsgIds = vals[i];
                        continue;
                    }
                    warnMsgIds += "," + vals[i];
                }
                if (warnMsgIds !== "") {
                    serialize += "&warnMsgIds=" + warnMsgIds;
                }
                $.operate.save(prefix + "/add", serialize);
            }
        }

        $("input[name='createdTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='updatedTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $(function (){
            $('.selectpicker').selectpicker();

            $("#selectDpName").hide();
            var tree = $.tree;
            $("#repositoryId").bind("change", function () {

                var repoId = $("#repositoryId option:selected").val();
                if (repoId === undefined || repoId === null || repoId === '') {
                    $("#selectDpName").hide();
                    $("#dpName").val(treeNode.name);
                    $("#fileId").val(fileId);
                    return;
                }
                $("#selectDpName").show();
                var url = ctx + "busi/repository_file/fileTreeData/"+repoId;
                var options = {
                    id: "fileTrees",
                    url: url,
                    check: { enable: false },
                    expandLevel: 0,
                    onClick: function (event, treeId, treeNode) {
                        if (treeNode.check_Child_State === -1) {
                            console.log("到了叶子结点")
                            var fileId = treeNode.id;
                            $("#dpName").val(treeNode.name);
                            $("#fileId").val(fileId);
                        }
                    }
                };
                tree.init(options);
            });

            $("#carteId").bind("change", function () {
                var carteId = $("#carteId option:selected").val();
                if (carteId === undefined || carteId === null || carteId === '') {
                    $("#dpPath").val(null);
                    return;
                } else {
                    $("#dpPath").val();
                }

                for (var i = 0; i < nodeList.length; i++) {
                    if (nodeList[i]["nodeId"] === carteId*1) {
                        $("#dpPath").val(nodeList[i]["nodeHost"]);
                    }
                }

            });

        });

        $('input').on('ifChanged', function(obj){
            var repoId = $("#repositoryId option:selected").val();
            if (repoId === undefined || repoId === null || repoId === '') return;
            var type = $(this).val();
            var checked = obj.currentTarget.checked;
            if (type == 1) {
                if (checked) {
                    $._tree.expandAll(true);
                } else {
                    $._tree.expandAll(false);
                }
            }
        })
    </script>
</body>
</html>